<!DOCTYPE html>
<html lang="en" style="font-size: 100px;">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
</body>
<script>
  function getArr(arr,brr){
    arr.forEach((item,index) => {
      brr.push(Object.assign(item))
      if(item.children){
        getArr(item.children,brr)
      }
    });
    return brr
  }
  const arr = [
    {
      name:'张三',
      children:[{
        name:'李四',
        children:[{
          name:'王五',
          children:[]
        }]
      }]
    },
    {
      name:'赵六'
    },
    {
      name:'周七',
      children:[{
        name:'吴八',
        children:[{
          name:'郑九'
        }]
      }]
    },
  ]
  
  const crr = getArr(arr,[]);
  let firstEndIndex
  crr.map((item,index)=>{
    if(item.name === arr[1].name){
      firstEndIndex = index-1
      crr[index-1].flagId = index-1
    }
  })
  console.log('arr',arr);
  console.log('crr',crr)
  console.log('firstEndIndex',firstEndIndex);
</script>
</html>